/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package testmanagementclient2;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 *ZipArchiveFile
 * 
 *  *
 * This is a prototype ZipArchiveFactory.  It is a huge work in progress still!
 *
 *
 * @author rrm46582
 */
public class ZAssignmentArchivePackager implements ArchivePackager<ZipInputStream, TestAssignment>
{

    public ZipInputStream pack(InputStream iStream)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public ZipInputStream pack(TestAssignment iAssignment)
    {
        try
        {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            ZipOutputStream oZip = new ZipOutputStream(buffer);
            ZipEntry eZip;

            String s = iAssignment.getInstructorSection();
            eZip = new ZipEntry("Assignment/Instructor.txt");
            oZip.putNextEntry(eZip);
            oZip.write(s.getBytes());

            s = iAssignment.getStudentSection();
            eZip = new ZipEntry("Assignment/Student.txt");
            oZip.putNextEntry(eZip);
            oZip.write(s.getBytes());

            s = iAssignment.getMetaData();
            eZip = new ZipEntry("Assignment/MetaData.txt");
            oZip.putNextEntry(eZip);
            oZip.write(s.getBytes());

            oZip.close();

            ByteArrayInputStream  targetStream = new ByteArrayInputStream(buffer.toByteArray());
            return new ZipInputStream(targetStream);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return null;
    }

    public ZipInputStream pack(byte[] iBytes)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }




    public byte[] toByteArray(ZipInputStream iZip)
    {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ZipOutputStream oZip = new ZipOutputStream(buffer);
        ZipEntry entry;

    
        try
        {
            while ( (entry = iZip.getNextEntry()) != null)
            {
                oZip.putNextEntry(entry);
                if ( ! entry.isDirectory())
                {
                    int size;
                    if ( (size = (int)entry.getSize()) != -1)
                    {
                        byte b[] = new byte[size];
                        iZip.read(b);
                        oZip.write(b);
                    }
                    else
                    {
                        byte b;
                        while ( (b = (byte)iZip.read()) != -1)
                            oZip.write(b);
                    }
                }
            }
            oZip.close();
            iZip.close();
            return buffer.toByteArray();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return null;
    }

    public InputStream toInputStream(ZipInputStream iZip)
    {
        return (InputStream)iZip;
    }

    public TestAssignment unpack(ZipInputStream iZip)
    {
        TestAssignment rAssignment = new TestAssignment();
        ZipEntry entry;

        try
        {
            while ( (entry = iZip.getNextEntry()) != null)
            {
                if ( entry.isDirectory() )
                    continue;
                else if ( entry.getName().compareTo("Assignment/Instructor.txt") == 0 )
                {
                    byte[] b = new byte[(int)entry.getSize()];
                    iZip.read(b);
                    rAssignment.setInstructorSection(new String(b));

                }
                else if ( entry.getName().compareTo("Assignment/Student.txt") == 0)
                {
                    byte[] b = new byte[(int)entry.getSize()];
                    iZip.read(b);
                    rAssignment.setStudentSection(new String(b));
                }
                else if ( entry.getName().compareTo("Assignment/MetaData.txt") == 0)
                {
                    byte[] b = new byte[(int)entry.getSize()];
                    iZip.read(b);
                    rAssignment.setMetaData(new String(b));
                }
            }
            iZip.close();
            return rAssignment;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return null;
    }

    public byte[] unpackAsByteArray(ZipInputStream iZip)
    {
        ZipEntry entry;
        ByteArrayOutputStream oStream = new ByteArrayOutputStream();

        try
        {
            while ( (entry = iZip.getNextEntry()) != null)
            {
                String s = entry.getName();
                byte[] stringBytes = s.getBytes();
                oStream.write(stringBytes, 0, stringBytes.length);

                if(! entry.isDirectory())
                {
                    byte[] b = new byte[(int)entry.getSize()];
                    iZip.read(b);
                    oStream.write(b);
                }
            }
            iZip.close();
            return oStream.toByteArray();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return null;
    }

    public InputStream unpackAsInputStream(ZipInputStream iZip)
    {
        return (InputStream) new ByteArrayInputStream(this.unpackAsByteArray(iZip));
    }


}
